# SPY Cam — ESP32-CAM + SH1106 OLED + PHP/MySQL  
*A step-by-step README from wiring to code to gallery*

---

## 0) What you’ll build

- An **ESP32-CAM** that takes a photo **every 30 seconds** (flash **OFF**), rotates it **90° clockwise**, JPEG-encodes it, and **uploads** to your server.
- An **OLED SH1106 (128×64 I²C)** to show status (“SPY Cam”, “Capturing…”, “Uploaded ✔”).
- A simple **PHP gallery (view.php)** that shows images in a 4-column grid, with **Retro look**, **Delete**, and **Delete All (TRUNCATE + reset counter)**.

---

## 1) Hardware

### Parts
- ESP32-CAM (AI Thinker module)
- SH1106 OLED 128×64 (I²C version)
- Jumper wires
- 5V power supply (stable)

### Wiring (ESP32-CAM ↔ OLED)
```
ESP32-CAM     →  SH1106 OLED
----------------------------
3V3           →  VCC
GND           →  GND
GPIO15 (SDA)  →  SDA
GPIO14 (SCL)  →  SCL
```
> I²C address 0x3C, SDA=15, SCL=14.

**Flash stays OFF**. Ensure stable 5V power.

---

## 2) Server (PHP/MySQL) setup

### Database Table
```sql
CREATE TABLE IF NOT EXISTS images (
  id INT AUTO_INCREMENT PRIMARY KEY,
  image LONGBLOB NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

### db.php (upload handler)
```php
<?php
$dbHost=""; 
$dbUser="";
$dbPass=""; 
$dbName="";
$mysqli = @new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($mysqli->connect_errno) { http_response_code(500); exit("DB Error"); }
$mysqli->set_charset("binary");
if (!isset($_FILES['image']) || $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
  http_response_code(400); exit("no image");
}
$blob = file_get_contents($_FILES['image']['tmp_name']);
$stmt = $mysqli->prepare("INSERT INTO images (image) VALUES (?)");
$stmt->bind_param("b", $blob);
$stmt->send_long_data(0, $blob);
$stmt->execute();
echo "OK id=".$stmt->insert_id;
$stmt->close(); $mysqli->close();
?>
```

### view.php (Gallery)
- Shows thumbnails in grid.
- Each image has Delete option.
- “Delete All” truncates table.

Use `header("Cache-Control: no-store, no-cache");` to avoid old images.

---

## 3) Arduino IDE setup

- Install **ESP32 core** (Boards Manager → “esp32 by Espressif Systems”).
- Board: **AI Thinker ESP32-CAM**
- Upload Speed: 115200
- Libraries: Adafruit GFX, Adafruit SH110X
- COM port connected to USB-UART (IO0→GND to flash).

---

## 4) ESP32-CAM Code

- Boot: “SPY Cam” text on OLED
- Every 30 seconds → capture → rotate → encode → upload
- Flash OFF permanently
- Unique filenames to prevent caching

(Use the full Camera3.ino code provided earlier.)

---

## 5) First Run

1. Power ESP32-CAM (5V stable).
2. Open Serial Monitor @115200.
3. Watch: “SPY Cam”, Wi-Fi connected.
4. Every 30s photo uploaded.
5. Open `view.php` → new image visible.

---

## 6) Troubleshooting

- **Camera init fail:** Check 5V, wiring.
- **Blank OLED:** Change address to 0x3D.
- **Old image shown:** Add cache-buster &v=time() or disable browser cache.
- **DB errors:** Check MySQL credentials, upload limits.

---

## 7) Customization

- Change interval: `SHOT_INTERVAL_MS`
- Add button trigger: connect GPIO13, use INPUT_PULLUP.
- Add NTP timestamps to filenames (optional).

---

## 8) Security

- Protect `view.php` + `db.php` with basic auth.
- Use secret key before TRUNCATE.
- Validate file type JPEG.

---

## 9) Quick Test

```bash
curl -F "image=@/path/to/photo.jpg" https://yourdomain/SupportingFiles/db.php
```
If you get `OK id=...`, server works.

---
